package com.Policing.CanniBalize;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.Policing.Ip_set.Ip_Set;
import com.Policing.Listview_animation.BounceLoadingView;
import com.Policing.R;
import com.Policing.Since_case.Since_case;
import com.Policing.bean.Department_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/11.
 */

public class Shift_site extends Activity {
    private SearchView sv;
    private ListView lv;
    private List<String> X=new ArrayList<>();
    private List<String > p = new ArrayList<String>();
    private String url;
    private BounceLoadingView loadingView;
    private Object[] names;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_category);
        //url = "http://"+ Ip_Set.IP+"/api/placeQuery"+ Since_case.set().getSource();
        url = "http://"+ Ip_Set.IP+"/api/placeQuery";
        //url = "http://192.168.1.159:8087/api/getAllPut";

        lv = (ListView) findViewById(R.id.lv_allot_sl);
        lv.setTextFilterEnabled(true);//设置lv可以被过虑
        sv = (SearchView) findViewById(R.id.tv_allot_sl);
        loadingView = (BounceLoadingView) findViewById(R.id.loadingView);
        loadingView.addBitmap(R.mipmap.v4);
        loadingView.addBitmap(R.mipmap.v5);
        loadingView.addBitmap(R.mipmap.v6);
        loadingView.addBitmap(R.mipmap.v7);
        loadingView.addBitmap(R.mipmap.v8);
        loadingView.addBitmap(R.mipmap.v9);
        loadingView.setShadowColor(Color.LTGRAY);
        loadingView.setDuration(700);
        loadingView.start();
        Thread1();
        // 设置该SearchView默认是否自动缩小为图标
        sv.setIconifiedByDefault(false);
        sv.setQueryHint("查找");
        // 为该SearchView组件设置事件监听器
        // 设置该SearchView显示搜索按钮
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 实际应用中应该在该方法内执行实际查询
                // 此处仅使用Toast显示用户输入的查询内容
                Toast.makeText(Shift_site.this, "您的选择是:" + query, Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Object[] obj = searchItem(newText);
                updateLayout(obj);
                return false;

            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = (String)adapterView.getItemAtPosition(i);//获取当前点击的item的字符串内容
                String id = "";
                for (int j = 0;j<X.size();j++){
                    if (X.get(j).equals(name.trim())){
                        id = p.get(j) ;
                    }

                }
                Intent intent = new Intent();
                intent.putExtra("site_name",name);
                intent.putExtra("site_id",id);
                Shift_site.this.setResult(3,intent);
                Shift_site.this.finish();//把item的内容返回给之前的activity
            }
        });

    }

    private void Thread1() {

        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
                     formBody.add("source",Since_case.set().getSource());

                    Request request = new Request.Builder()
                            .url(url)
                            .post(formBody.build())
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    showResponse(responseData);

                } catch (IOException e) {
                    e.printStackTrace();

                }


            }
        }.start();
    }

    private void showResponse(final String responseData) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Gson gson = new Gson();
                List<Department_bean> userList = gson.fromJson(responseData, new TypeToken<List<Department_bean>>(){}.getType());
                for(int i = 0;i < userList.size();i++){
                    X.add(userList.get(i).getName());
                    p.add(userList.get(i).getId());
                }
                //----------------------------------------------
                loadingView.setVisibility(View.GONE);
                names = X.toArray();
                lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
                        R.layout.listview_style,names));

            }
        });

    }
    public void updateLayout(Object[] obj) {
        lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
                R.layout.listview_style, obj));
    }
    public Object[] searchItem(String name) {
        ArrayList<String> mSearchList = new ArrayList<String>();
        for (int i = 0; i < X.size(); i++) {
            int index = X.get(i).indexOf(name);
            // 存在匹配的数据
            if (index != -1) {
                mSearchList.add(X.get(i));
            }
        }
        return mSearchList.toArray();
    }
}
